package com.winenine999.admin.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.winenine999.model.Page;
import com.winenine999.model.Purchaseordersub;
import com.winenine999.service.IPurchaseordersubService;
import com.winenine999.service.impl.PurchaseordersubService;

@Controller
@RequestMapping(value = "/purchaseordersub")
public class PurchaseordersubController {
	@Autowired
	private IPurchaseordersubService purchaseordersubService;
	@Autowired
	private IPurchaseordersubService PurchaseordersubService;
	/**
	 * 进入list页面
	 * @return
	 */
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String list(){
	
		return "message/purchaseordersub";	
	}
	/**
	 * 分页查询
	 * 一条sql
	 * @param start
	 * @param length
	 * @param total
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value="/list",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> findlist(Integer start, Integer length,String searchSyuserName,String searchBypurchaseName)
			throws UnsupportedEncodingException {
		if (start == null) {
			start = 0;
		};
		if (length == null) {
			length = 5;
		};
		if(searchSyuserName!=null){
			searchSyuserName=searchSyuserName.trim();
		}
		if(searchBypurchaseName!=null){
			searchBypurchaseName=searchBypurchaseName.trim();
		}
		// 查总条数（单表查询），先去联系怎删改
		Page page=new Page(start,length);
		Map<String,Object> params=new HashMap();
		params.put("searchSyuserName", searchSyuserName);
		params.put("searchBypurchaseName", searchBypurchaseName);
		page.setParams(params);
		page=purchaseordersubService.findPurchaseordersub(page);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("aaData", page.getList());//返回的数据
		map.put("iTotalRecords", page.getTotal());//总条数
		map.put("iTotalDisplayRecords",page.getTotal());////总条数
		return map;
	}
	/**
	 * 进入purchaseordersub2.jsp页面
	 * @return
	 */
	@RequestMapping("/purchaseordersub2")
	public String in(Integer phid,Model model) {
		model.addAttribute("phid", phid);
		return "message/purchaseordersub2";
	}
	@RequestMapping("/getinfo")
	@ResponseBody
	public Map<String, Object> getinfo(Model model, Purchaseordersub purchaseordersub, Integer start, Integer length) {
		System.out.println("=============进入controller=================" + purchaseordersub);
		/*
		 * Map<String,Integer> map=new HashMap<String,Object>(); map.put("phid",
		 * phid); map.put("uid", uid); System.out.println(map);
		 */
		Map<String, Object> map = new HashMap<String, Object>();
		Page page = new Page(start, length);
		map.put("purchaseordersub", purchaseordersub);
		// map.put("phid", purchaseordersub.getPhid());
		// map.put("uid", purchaseordersub.getUid());
		page.setParams(map);
		Page page1 = PurchaseordersubService.findPageOrderinfos(page);
		System.out.println("page1:" + page1);
		map.put("aaData", page1.getList());// 查到的结果集
		map.put("recordsFiltered", page1.getTotal());// 总条数，用于计算页数
		map.put("recordsTotal", page1.getTotal());// 总条数，用于页面显示：‘全部记录数 条’
		model.addAttribute("purchaseordersub", purchaseordersub);
		map.put("purchaseordersub", purchaseordersub);

		return map;

	}

	/**
	 * 酒数量的修改 订单的添加
	 * 
	 * @param purchaseordersub
	 * @return
	 */
	@RequestMapping("/wineNumSubmit")
	@ResponseBody
	public Integer wineNumSubmit(Purchaseordersub purchaseordersub) {
		System.out.println("====wineNumSubmit=====" + purchaseordersub + "=====================");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("phid", purchaseordersub.getPhid());
		map.put("uid", purchaseordersub.getUid());
		map.put("wid", purchaseordersub.getWid());
		map.put("num", purchaseordersub.getNum());
		boolean flag = PurchaseordersubService.wineOrderIsExistOrNot(map);
		if (flag) {
			System.out.println("order存在！");
			return PurchaseordersubService.updateWineNum(map);
		} else {
			System.out.println("order不存在！");
			return PurchaseordersubService.insertWineOrder(map);

		}
	}

	/**
	 * 批量 酒数量的修改 订单的添加
	 * 
	 * @param List<Map<String,Object>>list
	 * @return
	 */
	@RequestMapping("/addOrUpdateWineOrderGroups")
	@ResponseBody
	public Integer addOrUpdateWineOrderGroups(Integer wids[], Integer nums[], Integer uid, Integer phid) {
		System.out.println("====addOrUpdateWineOrderGroups==========================");
		System.out.println("nums"+nums.toString());
		System.out.println("nums"+wids.toString());
		int count = 0;
		for (int i = 0; i < nums.length; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("phid", phid);
			map.put("uid", uid);
			map.put("wid", wids[i]);
			map.put("num", nums[i]);

			boolean flag = PurchaseordersubService.wineOrderIsExistOrNot(map);
			if (flag) {
				System.out.println("order存在！");
				int n = PurchaseordersubService.updateWineNum(map);
				if (n == 1) {
					count += (int) map.get("num");
				}
				System.out.println("修改了" + n + "条数据");

			} else {
				System.out.println("order不存在！");
				int m = PurchaseordersubService.insertWineOrder(map);
				if (m == 1) {
					count += (int) map.get("num");
				}
				System.out.println("修改了" + m + "条数据");
			}
		}
		return count;

	}

	@RequestMapping("/deleteWineOrder")
	@ResponseBody
	public Integer deletewineOrder(Purchaseordersub purchaseordersub) {
		System.out.println("====wineDelete=====" + purchaseordersub + "=====================");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("phid", purchaseordersub.getPhid());
		map.put("uid", purchaseordersub.getUid());
		map.put("wid", purchaseordersub.getWid());
		return PurchaseordersubService.deletewineOrder(map);

	}

	/**
	 * 批量删除
	 * 
	 * @param purchaseordersub
	 * @return
	 */
	@RequestMapping("/deleteWineOrderGroups")
	@ResponseBody
	public Integer deleteWineOrderGroups(Integer wids[], Integer uid, Integer phid) {
		System.out.println("进入deleteWineOrderGroups==================");
		int count = 0;
		for (int i = 0; i < wids.length; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("phid", phid);
			map.put("uid", uid);
			map.put("wid", wids[i]);
			count += PurchaseordersubService.deletewineOrder(map);
		}
		return count;

	}

}
